<?php
class Game_model extends CI_Model {
	/**
	CREATE TABLE game (
	id int(11) NOT NULL AUTO_INCREMENT,
	title varchar(128) NOT NULL,
	code varchar(128) NOT NULL,
	fileSize int(10),
	jar varchar(128),
	jad varchar(128),
	img varchar(128),
	intro text,
	content text,
	PRIMARY KEY (id),
	KEY code (code)
	);
	*/
	
	public function __construct()
	{
		$this->load->database();
	}
	
	public function get_games($code = FALSE)
	{
		if ($code === FALSE)
		{
			$query = $this->db->get('game');
			return $query->result_array();
		}
		
		$query = $this->db->get_where('game', array('code' => $code));
		return $query->row_array();
	}
	
	public function get_game_by_id($id)
	{
		$query = $this->db->get_where('game', array('id' => $id));
		return $query->row();
	}
	
	public function set_game()
	{
		$this->load->helper('url');
		
		$code = $this->input->post('code');
		if($code === '')
		{
			$code = url_title($this->input->post('title'), 'dash', TRUE);
		}
		
		$data = array(
			'title' => $this->input->post('title'),
			'code' => $code,
			'fileSize' => $this->input->post('fileSize'),
			'jar' => $this->input->post('jar'),
			'jad' => $this->input->post('jad'),
			'img' => $this->input->post('img'),
			'intro' => $this->input->post('intro'),
			'content' => $this->input->post('content')
		);
		if ($this->input->post('id') > 0)
		{
			$this->db->where('id', $this->input->post('id'));
			return $this->db->update('game', $data);
		}
		
		return $this->db->insert('game', $data);
	}
}